import inspect

from common.base.base_api import BaseApi
from services.control.schemas.control_registers import ControlStates
from common.utils import make_literal
from settings import settings


class ControlApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(ControlApi, self).__init__(request_queue_uuid=settings.CONTROL_REQUEST_QUEUE)

    async def set_control_state(self, name: make_literal(ControlStates), state: bool, gain_back_brake: bool = False) -> bool | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'name': name, 'state': state, 'gain_back_brake': gain_back_brake}
        )

    async def get_all_control_states(self) -> ControlStates | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: ControlStates(**result)
        )

    async def get_control_state(self, name: make_literal(ControlStates)) -> bool | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'name': name})
